$(function () {
    $("#jqGrid").jqGrid({
        url: baseURL + 'sys/jxbanner/list',
        datatype: "json",
        colModel: [
            {label: 'id', name: 'id', index: 'id', width: 50, key: true},
            {
                label: '当前图片地址',
                name: 'imgUrl',
                index: 'img_url',
                width: 80,
                formatter: function (cellvalue, options, rowObject) {
                    return "<img src=" + cellvalue + " style='width: 200px; height: 120px' />";
                },
            },
            {label: '标题', name: 'title', index: 'title', width: 80},
            {label: '点击允许跳转地址', name: 'linkUrl', index: 'link_url', width: 80},
            {label: '排序', name: 'orderNum', index: 'order_num', width: 80},
            {
                label: 'banner 位置',
                name: 'type',
                index: 'type',
                width: 80,
                formatter: function (cellvalue, options, rowObject) {
                    if (cellvalue == "1") {
                        return "主页上方";
                    }
                    if (cellvalue == "2") {
                        return "主页中间";
                    }
                    if (cellvalue == "3") {
                        return "主页下方";
                    }
                    if (cellvalue == "4") {
                        return "实验室上方";
                    }
                    if (cellvalue == "5") {
                        return "资料上方";
                    }
                    return "";
                }
            }
        ],
        viewrecords: true,
        height: 385,
        rowNum: 10,
        rowList: [10, 30, 50],
        rownumbers: true,
        rownumWidth: 25,
        autowidth: true,
        multiselect: true,
        pager: "#jqGridPager",
        jsonReader: {
            root: "page.list",
            page: "page.currPage",
            total: "page.totalPage",
            records: "page.totalCount"
        },
        prmNames: {
            page: "page",
            rows: "limit",
            order: "order"
        },
        gridComplete: function () {
            //隐藏grid底部滚动条
            $("#jqGrid").closest(".ui-jqgrid-bdiv").css({"overflow-x": "hidden"});
        }
    });

    vm.imgUrl=$("#video").attr("src");
    new AjaxUpload('#upload', {
        action: baseURL + "sys/oss/upload",
        name: 'file',
        autoSubmit: true,
        responseType: "json",
        onSubmit: function (file, extension) {
            if (!(extension && /^(jpg|jpeg|png|gif)$/.test(extension.toLowerCase()))) {
                alert('请上传正确的图片格式!');
                return false;
            }

        },
        onComplete: function (file, r) {
            if (r.code == 0) {
                alert(r.url);
                vm.jxBanner.imgUrl = r.url;
                $("img").attr("src", r.url);

            }
        }
    });

});

var vm = new Vue({
    el: '#rrapp',
    data: {
        showList: true,
        title: null,
        jxBanner: {},
        imgUrl:"",
        imgWh:"1920x448"
    },
    methods: {
        query: function () {
            vm.reload();
        },
        add: function () {
            vm.showList = false;
            vm.title = "新增";
            vm.jxBanner = {};
            $("#video").attr("src",vm.imgUrl);
        },
        update: function (event) {
            var id = getSelectedRow();
            if (id == null) {
                return;
            }
            vm.showList = false;
            vm.title = "修改";

            vm.getInfo(id)
        },
        saveOrUpdate: function (event) {
            debugger;
            $('#btnSaveOrUpdate').button('loading').delay(1000).queue(function () {

                var url = vm.jxBanner.id == null ? "sys/jxbanner/save" : "sys/jxbanner/update";
                $.ajax({
                    type: "POST",
                    url: baseURL + url,
                    contentType: "application/json",
                    data: JSON.stringify(vm.jxBanner),
                    success: function (r) {
                        if (r.code === 0) {
                            layer.msg("操作成功", {icon: 1});
                            vm.reload();
                            $('#btnSaveOrUpdate').button('reset');
                            $('#btnSaveOrUpdate').dequeue();
                        } else {
                            layer.alert(r.msg);
                            vm.reload();
                            $('#btnSaveOrUpdate').button('reset');
                            $('#btnSaveOrUpdate').dequeue();
                        }
                    }
                });
            });
        },
        del: function (event) {
            var ids = getSelectedRows();
            if (ids == null) {
                return;
            }
            var lock = false;
            layer.confirm('确定要删除选中的记录？', {
                btn: ['确定', '取消'] //按钮
            }, function () {
                if (!lock) {
                    lock = true;
                    $.ajax({
                        type: "POST",
                        url: baseURL + "sys/jxbanner/delete",
                        contentType: "application/json",
                        data: JSON.stringify(ids),
                        success: function (r) {
                            if (r.code == 0) {
                                layer.msg("操作成功", {icon: 1});
                                $("#jqGrid").trigger("reloadGrid");
                            } else {
                                layer.alert(r.msg);
                            }
                        }
                    });
                }
            }, function () {
            });
        },
        getInfo: function (id) {
            $.get(baseURL + "sys/jxbanner/info/" + id, function (r) {
                vm.jxBanner = r.jxBanner;
                $("#video").attr("src", r.jxBanner.imgUrl);
            });
        },
        reload: function (event) {
            vm.showList = true;
            var page = $("#jqGrid").jqGrid('getGridParam', 'page');
            $("#jqGrid").jqGrid('setGridParam', {
                page: page
            }).trigger("reloadGrid");
            location.reload();
        },changeType:function (e) {
            var val = e.target.value;
            console.log(val);
            if (val=="1"){
                vm.imgWh="1920x448";
            }if (val=="2"){
                vm.imgWh="1200x640";
            }if (val=="3"){
                vm.imgWh="1200x448";
            }
            if (val=="4"){
                vm.imgWh="1920x448";
            }
            if (val=="5"){
                vm.imgWh="1920x448";
            }
        }
    }
});